package com.pxst.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.pxst.entity.Insert;
import com.pxst.entity.Update;
import com.pxst.exception.BusinessException;
import com.pxst.resp.PageResp;
import com.pxst.sys.req.PasswordReq;
import com.pxst.sys.resp.UserResp;
import com.pxst.utils.SecurityFrameworkUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.pxst.annotations.LogOperate;
import com.pxst.resp.ApiResp;
import com.pxst.sys.entity.UserEntity;
import com.pxst.sys.req.UserCreateReq;
import com.pxst.sys.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.validation.ValidationException;
import java.util.HashMap;
import java.util.Map;

/** 
* @author yangxy
* @version 创建时间：2023年7月26日 下午6:23:36 
*/
@RestController
@RequestMapping("/sys/user")
@Api(tags = "用户管理")
public class UserController {
	@Autowired
	private UserService userService;

	@Autowired
	private BCryptPasswordEncoder bCryptPasswordEncoder;
	
	@PostMapping
	@ApiOperation(value = "新增用户")
	@LogOperate(operate = "新增用户", service = UserService.class, type = "add")
	// @PreAuthorize("@ss.hasAnyPermissions('system:user:create')")
	public ApiResp<UserEntity> create(@RequestBody @Validated(Insert.class) UserCreateReq userCreateParam) {
		return userService.create(userCreateParam);
	}

	@PutMapping("/{userId}")
	@ApiOperation(value = "更新用户")
	@LogOperate(operate = "更新用户", service = UserService.class, type = "edit")
	// @PreAuthorize("@ss.hasAnyPermissions('system:user:update')")
	public ApiResp<UserEntity> update(@PathVariable("userId") Long userId, @RequestBody @Validated(Update.class) UserCreateReq userCreateParam) {
		if (StringUtils.isEmpty(userCreateParam.getUserName()) || StringUtils.isEmpty(userCreateParam.getFullName())) {
			Map map = new HashMap<String, Integer>();
			map.put("status", userCreateParam.getStatus());
			return userService.updateFields(userId, map);
		}
		return userService.update(userId, userCreateParam);
	}

	@GetMapping
	@ApiOperation(value = "查询用户")
	// @NoAuthorize
	public ApiResp<PageResp<UserResp>> getAll(UserCreateReq userCreateParam) {
		Integer currencyUnion = userCreateParam.getCurrencyUnion();
		if (currencyUnion == null){
			throw new ValidationException("币种查询方式不能为空");
		}
		return userService.getAll(userCreateParam);
	}


	@PutMapping("/password/change")
	@ApiOperation(value = "用户修改密码")
	@LogOperate(operate = "用户修改密码", service = UserService.class, type = "edit")
	public ApiResp passwordChange(@RequestBody PasswordReq passwordReq){
		String confirmPassword = passwordReq.getConfirmPassword();
		String newPassword = passwordReq.getNewPassword();
		String oldPassword = passwordReq.getOldPassword();
		if (!confirmPassword.equals(newPassword)){
			// 两次密码的输入不同
			throw new BusinessException("The two passwords are entered differently");
		}
		if (oldPassword.equals(newPassword)){
			// 旧密码和新密码必须不同
			throw new BusinessException("The old password must be different from the new password");
		}
		Map fieldMap = new HashMap<String, Integer>();
		fieldMap.put("password", bCryptPasswordEncoder.encode(newPassword));
		fieldMap.put("oldPassword", oldPassword);
		return userService.updateFields(SecurityFrameworkUtils.getLoginUserId(), fieldMap);
	}
}
